package io.jft.doll.ui.dollDetail

import android.app.AlertDialog
import android.arch.lifecycle.ViewModelProviders
import android.content.DialogInterface
import android.os.Bundle
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.g.base.extend.observeExOnce
import com.g.base.extend.progressDialog
import com.g.base.extend.resultNotNull
import com.g.base.help.observeSimpleNotify
import com.g.base.room.entity.UserEntity
import com.g.base.room.repository.Status
import com.g.base.router.RouteExtras
import com.g.base.ui.BaseActivity
import io.jft.doll.R
import io.jft.doll.databinding.ActivityDollDetailBinding
import io.jft.doll.event.NotifyUrl
import io.jft.doll.model.viewModel.DollViewModel
import io.jft.doll.room.entity.remote.DollDetailREntity
import io.jft.doll.room.entity.remote.GameHistoryListREntity
import io.jft.doll.router.RouterPage
import io.jft.doll.ui.gameHistory.bind.GameHistoryBind
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * Created by G on 2017/12/7 0007.
 */
@Route(path = RouterPage.DollDetailActivity, extras = RouteExtras.NeedOauth)
class DollDetailActivity : BaseActivity<ActivityDollDetailBinding>() {
    override var hasToolbar: Boolean = true
    private val dollViewModel by lazy { ViewModelProviders.of(this).get(DollViewModel::class.java) }
    lateinit var data: DollDetailREntity
    var dollPreviewBind: GameHistoryBind? = null
    var tipList = arrayListOf("数量", "编号", "状态", "兑换娃娃币数量", "物流名称", "物流单号")

    @JvmField
    @Autowired
    var id: String = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ARouter.getInstance().inject(this)
        setContentView(R.layout.activity_doll_detail)
        initView()
        getData()

        observeSimpleNotify<Any?>(this, NotifyUrl.DollDetailActivity) {
            getData()
            true
        }
    }

    private fun initView() {
        toolbar.title = "娃娃详情"
        toolbar.setNavigationIcon(R.drawable.ic_arrow_back_toolbar_24dp)
        toolbar.setNavigationOnClickListener {
            finishAfterTransition()
        }
    }

    override fun onReload() {
        getData()
    }

    override fun onTokenChange(data: UserEntity?) {
        if (data == null) {
            showNeedOauth()
        } else {
            getData()
        }
    }

    private fun getData() {
        dollViewModel.getDollDetail(id)
                .resultNotNull()
                .observeExOnce(this) {
                    when (it.status) {
                        Status.Loading -> showLoading()
                        Status.Error -> showError()
                        Status.Content -> {
                            data = it.data!!
                            dollPreviewBind = GameHistoryBind(GameHistoryListREntity(data.wawa.img, data.wawa.title, "", 2, data.wawa.date), false)
                            contentView.data = this
                            showContentView()
                        }
                    }
                }
    }

    fun onCoinClick(view: View) {
        AlertDialog.Builder(this)
                .apply {
                    setTitle("兑换娃娃币")
                    setMessage("兑换娃娃币后不能申请发货了,确定要兑换吗")
                    setCancelable(true)
                    setPositiveButton("确定") { _: DialogInterface, _: Int ->
                        dollViewModel.dollToCoin(id)
                                .progressDialog({ it.dismiss(true) }, { it.dismiss(true) })
                                .observeExOnce(this@DollDetailActivity) {
                                    if (it.status == Status.Content) {
                                        EventBus.getDefault().post(DollStatusChangeEvent(5, id))
                                    }
                                }
                    }
                    setNegativeButton("取消", null)
                    show()
                }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onStatusChangeEvent(ev: DollStatusChangeEvent) {
        if (ev.id != id) return
        data.wawa.status = ev.status
        contentView.data = this@DollDetailActivity
    }

    fun onPostageClick(view: View) {
        ARouter.getInstance().build(RouterPage.CartActivity).navigation()
    }

    fun getStatus(status: Int) =
            when (status) {
                1 -> "寄存中"
                2 -> "待发货"
                3 -> "待收货"
                4 -> "已领养"
                5 -> "兑换娃娃币"
                else -> ""
            }
}

class DollStatusChangeEvent(val status: Int, val id: String)